package gui;

import effect.Effect;
import gui.GUIConstants.SelectionStatus;
import gui.GUIConstants.ViewState;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import card.Card;
import core.Constants;
import core.MapUtil;
import core.UpdateListener;

public class GUICore extends Observable{
	private static GUICore me;
	private Point mapOffset=new Point(0, 0);
	private List<UpdateListener> listeners = new ArrayList<UpdateListener>();
	private GUITile[][] guiMap;
	public List<SelectionSphere> selectionSpheres = new ArrayList<SelectionSphere>();
	private Card selectedCard;
	private Effect selectedEffect;
	public SelectionStatus selectionStatus = SelectionStatus.NO_SELECTION;
	public ViewState currentView = ViewState.MAP;
	
	public static GUICore getInstance(){
		if(me==null){
			me=new GUICore();
		}
		return me;
	}

	private GUICore(){
		renewMap();
		
	}
	
	/**
	 * Should only be called once by the constructor.
	 */
	private void renewMap(){
		guiMap=MapUtil.getGUIMap();
	}
	public GUITile[][] getGUIMap(){
		return guiMap;
	}
	public void setSelectedCard(Card c){
		selectedCard=c;
		setChanged();
		notifyObservers();
	}
	/** May return null **/
	public Card getSelectedCard(){
		return selectedCard;
	}
	public void setSelectedEffect(Effect e){
		selectedEffect=e;
		setChanged();
		notifyObservers();
	}
	/** May return null **/
	public Effect getSelectedEffect(){
		return selectedEffect;
	}
	public List<GUITile> getAllGUITiles(){
		List<GUITile> result = new ArrayList<GUITile>();
		for(int x=0; x<Constants.MAPSIZE; x++){
			for(int y=0; y<Constants.MAPSIZE; y++){
				result.add(guiMap[x][y]);
			}
		}
		return result;
	}
	public void setMapOffset(Point offset){
		mapOffset=offset;
		for(GUITile t : getAllGUITiles()){
			t.setOffset(offset);
		}
		updateAll();
	}
	public Point getMapOffset(){
		return mapOffset;
	}
	public void addUpdateListener(UpdateListener l){
		listeners.add(l);
	}
	public void updateAll(){
		for(UpdateListener l : listeners){
			l.update();
		}
	}
}
